package com.mq.rabbitmq.point;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
/**
 * Created by Administrator on 2017/9/28.
 */
//@Configuration
public class SecondConfig {
//    @Bean("secondAdmin")
//    public RabbitAdmin secondAdmin(){
//        return new RabbitAdmin(secondConnectionFactory());
//    }

    /**
     * 在使用RabbitMQ的时候，我们可以通过消息持久化操作来解决因为服务器的异常奔溃导致的消息丢失，除此之外我们还会遇到一个问题，当消息的发布者在将消息发送出去之后，消息到底有没有正确到达broker代理服务器呢？如果不进行特殊配置的话，默认情况下发布操作是不会返回任何信息给生产者的，
     * 也就是默认情况下我们的生产者是不知道消息有没有正确到达broker的，如果在消息到达broker之前已经丢失的话，持久化操作也解决不了这个问题，因为消息根本就没到达代理服务器，你怎么进行持久化，那么这个问题该怎么解决呢？
     * @return
     */
    @Bean(name="secondConnectionFactory")
    public ConnectionFactory secondConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setAddresses("127.0.0.1:5672");
//        connectionFactory.setUsername("guest");
//        connectionFactory.setPassword("guest");

        String rabbitmq1="192.168.2.47:5672";
        String rabbitmq2="192.168.2.48:5672";
        connectionFactory.setAddresses(rabbitmq1+","+rabbitmq2);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("rabbitmqadmin");
        connectionFactory.setChannelCacheSize(100);
        connectionFactory.setChannelCheckoutTimeout(200);
        // connectionFactory.setConnectionCacheSize(1000);

        //服务器发送给正常的消费者成功了，会回调，不然的话，生产者不知道是否服务器正常推送给消费者了。
        //消费者是否正常消费了，假如持久化失败，
        //消息服务器异常，导致该消息丢失
        connectionFactory.setPublisherConfirms(true); //必须要设置
        return connectionFactory;
    }


    @Bean
    @Primary
    public RabbitTemplate rabbitTemplate(
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory){
        RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
        return firstRabbitTemplate;
    }

    @Bean(name="secondRabbitTemplate")
    public RabbitTemplate secondRabbitTemplate(@Qualifier("secondConnectionFactory")
                                                   ConnectionFactory connectionFactory){
        RabbitTemplate secondRabbitTemplate = new RabbitTemplate(connectionFactory);
        return secondRabbitTemplate;
    }

    @Bean(name="firstListener")
    public SimpleRabbitListenerContainerFactory firstFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean(name="secondListner")
    public SimpleRabbitListenerContainerFactory secondFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean
    public Queue firstQueue() {
        System.out.println("configuration firstQueue ........................");
        return new Queue("hello1");
    }

    @Bean
    public Object secondQueue() {
        System.out.println("2222configuration secondQueue ........................");
        return new Queue("hello2");
    }
}
